package guzb.cnblogs.security.industrydemo.util;

/**
 * JSON格式的响应
 */
public class JsonResponse<T> {

    private int code;

    private String message;

    private T data;

    private String errorMessage;

    public static final JsonResponse OK = JsonResponse.ok(null);

    public static final JsonResponse ERROR = JsonResponse.error(500, "操作失败");

    public static final <T> JsonResponse<T> ok(T data) {
        return ok(data, "操作成功");
    }

    public static final <T> JsonResponse<T> ok(T data, String message) {
        JsonResponse ok = new JsonResponse();
        ok.code = 200;
        ok.data = data;
        ok.message = message;
        return ok;
    }

    public static final <T> JsonResponse<T> err400(String errorMessage) {
        return error(400, errorMessage);
    }

    public static final <T> JsonResponse<T> err401(String errorMessage) {
        return error(401, errorMessage);
    }

    public static final <T> JsonResponse<T> err500(String errorMessage) {
        return error(500, errorMessage);
    }

    public static final <T> JsonResponse<T> error(int code, String errorMessage) {
        JsonResponse error = new JsonResponse();
        error.code = code;
        error.errorMessage = errorMessage;
        return error;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public T getData() {
        return data;
    }
}
